<div class="postTerms index">
<?php if(defined($status)): ?>
    <span class="ajaxstatus"><?php echo $status; ?></span>
<?php endif; ?>
<h2><?php __('Manage PostTerms');?></h2>
<!-- Left Fat Section -->
<div class="widfats">
    <!-- Table Navigation -->
    <div class="tablenav">
        <div class="batchactions"></div>
        <?php echo $this->element('pager'); ?>
    </div>
    <!-- Table Content Area -->
    <table id="table_post_term" class="table fixed">
        <thead title="<?php __('Click a column to sort data table'); ?>">
            <tr>
                <th width="5%" class="checkbox" title="<?php __('Click to select/deselect all records'); ?>"><input type="checkbox" class="checkbox"/></th>
                <th width="10%"><?php echo $paginator->sort('id');?></th>
                <th width="35%"><?php echo $paginator->sort('name');?></th>
                <th width="20%"><?php echo $paginator->sort('post_count');?></th>
                <th width="30%" class="actions"><?php __('Actions');?></th>
            </tr>
        </thead>
        <tfoot title="<?php __('Click a column to sort data table'); ?>">
            <tr>
                <th class="checkbox" title="<?php __('Click to select/deselect all records'); ?>"><input type="checkbox" class="checkbox"/></th>
                <th><?php echo $paginator->sort('id');?></th>
                <th><?php echo $paginator->sort('name');?></th>
                <th><?php echo $paginator->sort('post_count');?></th>
                <th class="actions"><?php __('Actions');?></th>
            </tr>
        </tfoot>
        <tbody>
            <?php foreach ($postTerms as $i=>$postTerm):
                $class = $i % 2 == 0 ? ' class="altrow"' : ''; ?>
            <tr<?php echo $class;?>>
                <td class="checkbox"><input type="checkbox" class="checkbox"/></td>
                <td><?php echo $postTerm['PostTerm']['id']; ?></td>
                <td><?php echo $postTerm['PostTerm']['name']; ?></td>
                <td><?php echo $postTerm['PostTerm']['post_count']; ?></td>
                <td class="actions">
                    <?php echo $html->link(__('View', true), array('action' => 'view', $postTerm['PostTerm']['id'])); ?>
                    <?php if($user['group'] != 'student'): ?>
                        <?php echo $html->link(__('Edit', true), array('action' => 'edit', $postTerm['PostTerm']['id'])); ?>
                        <?php echo $html->link(__('Delete', true), array('action' => 'delete', $postTerm['PostTerm']['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $postTerm['PostTerm']['id'])); ?>
                    <?php endif; ?>
                </td>
            </tr>
            <?php endforeach; ?>
        <tbody>
    </table>
    <!-- PostTerm View Section -->
    <div id="posts_by_term"></div>
</div>
<!-- Sidebar Section -->
<div class="sidebar">
    <!-- PostTerm Add Form -->
    <div id="widget_add_post_term" class="widget">
        <div class="toggle">&nbsp;</div>
        <h3 class="handle"><span><?php __('Add PostTerm');?></span></h3>
        <div class="content">
            <div class="postTerms form">
            <!-- PostTerm Add Form -->
            <?php e($form->create('PostTerm', array('action'=>'add'))); ?>
                <fieldset>
                    <?php e($form->input('name', array('label'=>__('PostTerm', true), 'class' => 'text input', 'autocomplete'=>'off'))); ?>
                    <?php e($form->submit(__('Add PostTerm', true), array('class' =>'submit attention'))); ?>
                </fieldset>
            <?php e($form->end()); ?>
            </div>
        </div>
    </div>
    <!-- PostTerm Edit Form -->
    <div id="widget_edit_post_term" class="widget">
        <div class="toggle">&nbsp;</div>
        <h3 class="handle"><span><?php __('Edit PostTerm');?></span></h3>
        <div class="content">
            <p class="faq"><?php __('There are no PostTerm to edit now'); ?></p>
        </div>
    </div>
    <!-- Quick Actions -->
    <div id="widget_quick_actions" class="widget">
        <div class="toggle">&nbsp;</div>
        <h3 class="handle"><span><?php __('Quick Actions');?></span></h3>
        <div class="content">
            <div class="actions">
                <ul>
                    <li><?php echo $html->link(__('List Posts', true), array('controller' => 'posts', 'action' => 'index')); ?> </li>
                    <li><?php echo $html->link(__('New Post', true), array('controller' => 'posts', 'action' => 'add')); ?> </li>
                    <br class="clear"/>
                </ul>
            </div>
        </div>
    </div>
</div>
</div>
<script type="text/javascript">
$(window).ready(function(){
    // Validate PostTermAddForm
    $.sme.data.post_term = false;
    $('#PostTermAddForm').submit(function(){
        if(!$(this).valid()){
            $.log('Add post term with invalid values, drop the request');
            return false;
        }
        if(!$.sme.status.post_term){
            alert('该栏目名已经存在, 不能添加');
            $.log('Post term name taken, use another');
            return false;
        }
    }).validate({
        success: function(label){
            $.sme.cbk.ajaxCheck.apply({
                label: label,
                input: label.prev('input'),
                id: 'PostTermName',
                model: 'PostTerm',
                control: 'post_term',
                fielden: 'name',
                fieldzh: '栏目名'
            });
        },
        rules: { 'data[PostTerm][name]': 'required' },
        messages: { 'data[PostTerm][name]': '栏目名称不能为空值' }
    });

    // Validate PostTermEditForm
    $('#PostTermEditForm').submit(function(){
        if(!$(this).valid()){
            $.log('Edit post term with invalid values, drop the request');
            return false;
        }
        if(!$.sme.status.post_term){
            alert('该栏目名已经存在, 不能添加');
            $.log('Post term name taken, use another');
            return false;
        }
        var target = $('div#widget_edit_post_term div.content');
        $(this).ajaxSubmit({
            global: false,
            target: target,
            beforeSend: function(){
                target.append($.sme.option.span);
            }
        });
        return false;
    }).validate({
        success: function(label){
            $.sme.cbk.ajaxCheck.apply({
                label: label,
                input: label.prev('input'),
                id: 'PostTermName',
                model: 'PostTerm',
                control: 'post_term',
                fielden: 'name',
                fieldzh: '栏目名'
            });
        },
        rules: { 'data[PostTerm][name]': 'required' },
        messages: { 'data[PostTerm][name]': '栏目名称不能为空值' }
    });

    // Bind PostTerm Edit callbacks
    $('table#table_post_term td.actions a[href*=edit]').click(function(event){
        event.preventDefault();
        if($.sme.data.currentPostTerm == this.href){
            $.log('Edit for the same post term, drop the request');
            return false;
        }
        $.sme.data.currentPostTerm = this.href;
        $.log('Edit PostTerm #'+this.href);
        $.ajax({
            url: this.href,
            global: false,
            beforeSend: function(){
                $('div#widget_edit_post_term').children('div.content').append($.sme.option.span);
            },
            success: function(content){
                $('div#widget_edit_post_term').children('div.content').html(content);
                $.sme.cbk.initPostTermEditForm();
            }
        });
    });

    // Bind PostTerm View callbacks
    $('table#table_post_term td.actions a[href*=view]').click(function(event){
        event.preventDefault();
        if($.sme.data.currentPostTerm == this.href){
            $.log('View for the same post term, drop the request');
            return false;
        }
        $.sme.data.currentPostTerm = this.href;
        $.log('Edit PostTerm #'+this.href);
        $.ajax({
            url: this.href,
            global: false,
            beforeSend: function(){
                $('div#posts_by_term').html($.sme.option.span);
            },
            success: function(content){
                $('div#posts_by_term').html(content);
            }
        });
    });
});

// Extend $.sme.cbk with some methods
$.extend($.sme.cbk, {
    initPostTermEditForm: function(){}
});
</script>